//	获取url传值
function getQueryString(name) {
	var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
	if (result == null || result.length < 1) {return "";}
		return result[1];
}
id = getQueryString("id");

$.validator.setDefaults({
    submitHandler: function() {
      	var params = $(".form-div").serialize()+'&goodsId='+id; 
      	$.ajax( {  
                type : "POST",  
                url : serverIP+"/user/goods/save",  
                data : params,  
                xhrFields: {
	                withCredentials: true    // 设置sessionId相同
	            },
                success : function(data) {
                	if(data.code==0){
                		alert("修改成功");
                	}else{
                		alert(data.msg);
                	}
                }  
        }); 
    }
});

$(document).ready(function() {
	$('.form-div').validate({
		rules: {
		 	num: {
		 		required: true,
		 		number:true,
		 		min:0
		 	}
		},
		messages: {
	      	num: {
	       		required: "请输入要入库的数量",
	        	number: "请输入数字",
	        	min:"入库数量不能不能为负数"
	      	}
		}	
	})
	
})
$(function(){
	if($(".admin").height()<600){
		$('.nav-btn').height('50px').find('img').hide();
	}else{
		$('.nav-btn').height('150px').find('img').show();
	}
	//	获取商品品牌
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"brand"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="brand"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
//	获取商品型号
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"model"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="model"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
//	获取计量单位
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"unit"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="unit"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
//	获取颜色
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"color"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="color"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
//	获取物料详情信息
	$.ajax({
		type:"get",
		url:serverIP+"/user/goods/view?goodsId="+id,
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
		success:function (data) {
			$('input[name="name"]').val(data.data.name);
			$('select[name="brand"]').append("<option value="+data.data.brand+">"+data.data.brandName+"</option>");
			$('select[name="model"]').append("<option value="+data.data.model+">"+data.data.modelName+"</option>");
			$('input[name="type"]').val(data.data.type);
			$('select[name="format"]').append("<option value="+data.data.format+">"+data.data.formatName+"</option>");
			$('select[name="unit"]').append("<option value="+data.data.unit+">"+data.data.unitName+"</option>");
			$('input[name="color"]').val(data.data.color);
			$('input[name="warnNum"]').val(data.data.warnNum);
		}
	})
})
//监听浏览器窗口变化
window.onresize = function(){
	var bodyH=$("body").height();
	$(".leftbar").height(bodyH-45+"px");
	$(".content").height(bodyH-45+"px");
	$(".admin").height($(".content").height()-45+"px");
	if($(".admin").height()<600){
		$('.nav-btn').height('50px').find('img').hide();
	}else{
		$('.nav-btn').height('150px').find('img').show();
	}
}
